﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.NewPB.IView;
using MINERP.MODEL.FoundationModel;
using MINERP.NewPB.Presenter;

namespace MINERP.NewPB.Forms
{
    public partial class FrmTemplate : Form, IFrmTemplateView
    {

        TemplatePresenter _presenter;
        public FrmTemplate()
        {
            InitializeComponent();
            _presenter = new TemplatePresenter(this);
        }

        #region IFrmTemplateView 成员

        public int SelectedTemplateId
        {
            get { return (int)cmbPaperTemplate.SelectedValue; }
        }

        public void BindTemplate(List<Tb_paperTemplate_Info> listTemplate)
        {
            cmbPaperTemplate.DataSource = listTemplate;
            cmbPaperTemplate.DisplayMember = "TemplateName";
            cmbPaperTemplate.ValueMember = "TempLateId";
        }

        public void ShowTemplateDetail(Tb_paperTemplate_Info curTemplate)
        {
            cmbPaperFace.Text = curTemplate.PaperFaceName;
            cmbPaperType.Text = curTemplate.PaperTypeName;
            cmbBoardLength.Text = curTemplate.BoardLength.ToString();
            cmbBoardHeight.Text = curTemplate.BoardHeight.ToString();
            chkIsBarcode.Checked = curTemplate.HasBarCode;
        }

        public string NewTemplateName
        {
            get { return cmbPaperTemplate.Text.Trim(); }
        }

        public int NewTPaperFaceId
        {
            get { return (int)cmbPaperFace.SelectedValue; }
        }

        public int NewTPaperTypeId
        {
            get { return (int)cmbPaperType.SelectedValue; }
        }

        public float NewTWidth
        {
            get { return Convert.ToSingle(cmbBoardLength.Text.Trim()); }
        }

        public float NewHeight
        {
            get { return Convert.ToSingle(cmbBoardHeight.Text.Trim()); }
        }

        public bool NewHasBarCode
        {
            get { return chkIsBarcode.Checked; }
        }

        public int SelectPaperTypeId
        {
            get { return (int)cmbPaperType.SelectedValue; }
        }

        public void BindPaperFace(List<PaperFace_Info> _listPaperFace)
        {
            cmbPaperFace.DataSource = _listPaperFace;
            cmbPaperFace.DisplayMember = "Name";
            cmbPaperFace.ValueMember = "PaperFaceID";
        }

        public void BindPaperType(List<PaperType_Info> _listPaperType)
        {
            cmbPaperType.DataSource = _listPaperType;
            cmbPaperType.DisplayMember = "Name";
            cmbPaperType.ValueMember = "PaperTypeID";
        }

        public void BindBoardWidth(List<BoardWidth_Info> _listBoardWidth)
        {
            cmbBoardHeight.DataSource = _listBoardWidth;
            cmbBoardHeight.DisplayMember = "Width";
            cmbBoardHeight.ValueMember = "WidthId";
        }

        public void Close(bool SusSelectT)
        {
            if (SusSelectT == true)
            {
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                this.DialogResult = DialogResult.No;
            }
            this.Close();
        }
        #endregion

        private void cmbPaperTemplate_DropDownClosed(object sender, EventArgs e)//选中模板，并显示明细
        {
            _presenter.SelectedOneTemplate();
        }

        private void btnOK_Click(object sender, EventArgs e)//选中某模板
        {
            _presenter.SelectTemplateOK();
        }

        private void btnDelTemplate_Click(object sender, EventArgs e)//删除模板
        {
            _presenter.DelTemplate();
        }

        private void button1_Click(object sender, EventArgs e)//修改模板
        {

            if (button1.Text == "修改")
            {
                //_presenter.BindOther();
                //cmbPaperTemplate.DropDownStyle = ComboBoxStyle.DropDown;
                cmbPaperFace.Enabled = true;
                cmbPaperType.Enabled = true;
                cmbBoardHeight.Enabled = true;
                cmbBoardLength.Enabled = true;
                btnDelTemplate.Enabled = false;
                btnAddTemp.Enabled = false;
                button1.Text = "保存";
            }
            else
            {
                _presenter.ModifyTemplate();
                //cmbPaperTemplate.DropDownStyle = ComboBoxStyle.DropDown;
                cmbPaperFace.Enabled = false;
                cmbPaperType.Enabled = false;
                cmbBoardHeight.Enabled = false;
                cmbBoardLength.Enabled = false;
                btnDelTemplate.Enabled = true;
                btnAddTemp.Enabled = true;
                button1.Text = "修改";
            }
        }

        private void btnAddTemp_Click(object sender, EventArgs e)//新增模板
        {

            if (btnAddTemp.Text == "新增")
            {
                //_presenter.BindOther();
                cmbPaperTemplate.DropDownStyle = ComboBoxStyle.DropDown;
                cmbPaperFace.Enabled = true;
                cmbPaperType.Enabled = true;
                cmbBoardHeight.Enabled = true;
                cmbBoardLength.Enabled = true;
                btnDelTemplate.Enabled = false;
                button1.Enabled = false;
                btnAddTemp.Text = "保存";
            }
            else
            {
                _presenter.AddNewTemplate();
                cmbPaperTemplate.DropDownStyle = ComboBoxStyle.DropDownList;
                cmbPaperFace.Enabled = false;
                cmbPaperType.Enabled = false;
                cmbBoardHeight.Enabled = false;
                cmbBoardLength.Enabled = false;
                btnDelTemplate.Enabled = true;
                button1.Enabled = true;
                btnAddTemp.Text = "新增";
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)//关闭
        {
            DialogResult = DialogResult.No;
            this.Close();
        }

        private void cmbBoardHeight_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                Convert.ToSingle(cmbBoardHeight.Text.Trim());
            }
            catch (Exception)
            {
                errorProvider1.SetError(cmbBoardHeight, "请输入数字");
            }
        }

        private void cmbBoardLength_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                Convert.ToSingle(cmbBoardLength.Text.Trim());
            }
            catch (Exception)
            {
                errorProvider1.SetError(cmbBoardLength, "请输入数字");
            }
        }

        private void cmbPaperType_DropDownClosed(object sender, EventArgs e)//选择纸型后绑定相应的纸宽
        {
            _presenter.BindPaperWidth();
        }
    }
}
